import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/Admin/User.vue'
import SystemInfo from '../components/Sjpt/SystemInfo.vue'
import TableMap from '../components/Sjpt/TableMap.vue'

Vue.use(VueRouter)
  
  const routes = [
    {
      path:"/",
      redirect:"/login"
    },
    {
      path:"/login",
      component:Login
    },
    {
      path:"/home",
      component:Home,
      redirect:"/welcome",
      children:[
        {path:"/welcome",component:Welcome,},
        {path:"/user",component:User,},
        {path:"/systeminfo",component:SystemInfo,},
        {path:"/taskdev",component:TableMap,}
      ]
    },
  ]

const router = new VueRouter({
  routes
}
)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(localtion,onResolve,onReject){
  if(onResolve||onReject) return originalPush.call(this,localtion,onResolve,onReject)
  return originalPush.call(this,localtion).catch(err => err)
}
// 导航守卫
router.beforeEach((to,from,next)=>{
  // to将要访问
  // from从哪访问
  // next接下来的处理 next(url)重定向到url上 next()继续访问 to 路径

  if(to.path=='/login') return next();
  const userflag = window.sessionStorage.getItem("user");
  if(!userflag) return next('/login');
  next();

} 
)

export default router
